package cn.xyinping.user.service.impl;

import cn.xyinping.basic.exception.MyException;
import cn.xyinping.basic.service.impl.BasicServiceImpl;
import cn.xyinping.basic.util.MD5Utils;
import cn.xyinping.basic.util.StrUtils;
import cn.xyinping.user.constant.UserConstant;
import cn.xyinping.user.domain.Logininfo;
import cn.xyinping.user.domain.User;
import cn.xyinping.user.dto.UserDto;
import cn.xyinping.user.mapper.LogininfoMapper;
import cn.xyinping.user.mapper.UserMapper;
import cn.xyinping.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author 27266
 * @desrciption
 * @date 2021/10/29
 */
@Service
public class UserServiceImpl extends BasicServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(UserDto userDto) {
//        ①非空校验，注册的四个参数
        if(StringUtils.isEmpty(userDto.getPhone())
                ||StringUtils.isEmpty(userDto.getCode())
                ||StringUtils.isEmpty(userDto.getPassword())
                ||StringUtils.isEmpty(userDto.getPasswordRepeat())){
            throw new MyException("参数不能为空");
        }
//        ②手机号是否被注册
        User phone = userMapper.findByPhone(userDto.getPhone());
        if(phone!=null){
            throw new MyException("该手机号已被注册");
        }
//        ③两次密码是否一致
        if(!userDto.getPassword().equals(userDto.getPasswordRepeat())){
            throw new MyException("两次输入的密码不一致");
        }
//        ④验证码校验
        Object obj = redisTemplate.opsForValue().get(UserConstant.REGISTER_PREFIX + userDto.getPhone()); // 从redis中获取验证码+时间戳
        if(obj==null){ // 判断是否过期
            throw new MyException("验证码已过期");
        }else {
            String code = obj.toString().split(":")[1]; // 从redis中获取验码
            if(!code.equalsIgnoreCase(userDto.getCode())){
                throw new MyException("验证码输入错误");
            }
        }
//        把数据保存到user, logininfo两张表，因为user表有外键，所以先保存logininfo表
        User user = userDtotouser(userDto);
        Logininfo logininfo = userDtoToLogininfo(user);
        logininfoMapper.add(logininfo); // 返回自增主键
        user.setLogininfo_id(logininfo.getId());
        userMapper.add(user);

    }


    /**
     * 获取logininfo对象
     * @param user
     * @return
     */
    private Logininfo userDtoToLogininfo(User user) {
        Logininfo logininfo = new Logininfo();
        // copy user中的数据到logininfo中
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);  // type:1 普通用户
        logininfo.setDisable(1);  // 启用状态
        return logininfo;
    }

    private User userDtotouser(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        user.setState(1);
        // 盐值
        String stalt = StrUtils.getComplexRandomString(32);
        // 加密加盐
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + stalt);
        user.setSalt(stalt);
        user.setPassword(password);
        return user;
    }
}
